package com.zhangxin.sunnyweather.logic.dao

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.provider.Settings.System.putString
import com.google.gson.Gson
import com.zhangxin.sunnyweather.SunnyWeatherApplication.Companion.context
import com.zhangxin.sunnyweather.logic.model.Place

object PlaceDao {

    private val sharedPreferences=context.getSharedPreferences("sunny_weather", Context.MODE_PRIVATE)

    @SuppressLint("CommitPrefEdits")
    fun savePlace(place: Place) {
        val edit = sharedPreferences.edit()
        edit.putString("place", Gson().toJson(place))
        edit.apply()
    }

    fun getSavedPlace(): Place {
        val placeJson =
            sharedPreferences.getString("place", "")
        return Gson().fromJson(placeJson, Place::class.java)
    }

    fun isPlaceSaved(): Boolean {
        return sharedPreferences.contains("place")
    }
}